home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7101 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: fclose() and remove() within a funct'n
  5. Date: 18 Feb 1996 17:06:06 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4g87se$rt0@umbc9.umbc.edu>
  8. References: <4fo8ps$3v6@milo.freenet.vancouver.bc.ca>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Patrick Wong <zipz@opus.freenet.vancouver.bc.ca> wrote:
  13. |> I'm writing a function that is supposed to close and remove temporary files
  14. |> on a DOS machine.  I'm posting this in c.l.c because it's a C question as 
  15. |> well as a DOS question.
  16.  
  17. As long as it's portable across all ANSI C compilers then it's perfectly
  18. acceptable to post it here.
  19.  
  20. |> I am using the atexit() function to close the temporary file before 
  21. |> exiting the function.  Yet the atexit() function will not allow me to 
  22. |> pass a pointer to the file that I want to close (and delete).  The 
  23. |> function MUST be responsible for closing and deleting all temporary files 
  24. |> it creates upon PROGRAM termination (hence the atexit() function).
  25.  
  26. I've never used atexit() before, but it is my understanding that upon
  27. program exit whatever was given to atexit() is executed. Assuming normal
  28. program exit. However the function passed to atexit() must take and
  29. return void.
  30.  
  31. |> Here is the function:
  32. |> 
  33. |> FILE *tempname(char *filename, char *mode)
  34. |> {
  35. |>     FILE *fptemp;
  36. |>     char *ptemp;
  37. |>     const char fnlen = 13;
  38. |> 
  39. |>     if ((ptemp = (char *)malloc(fnlen * sizeof(char))) == NULL)
  40.  
  41. The cast is not necessary and if you do not #include <stdlib.h> then
  42. the cast will actually hide a flaw in your program. The sizeof() is not
  43. needed either since a char is guaranteed to be 1 in ANSI C.
  44.  
  45. |>       return NULL;
  46. |>     ptemp = (filename == NULL) ? tmpnam(NULL) : tmpnam(filename);
  47. |>     return (((fptemp = fopen(filename, mode)) == NULL) ? NULL : fptemp);
  48.  
  49. Wow I thought I was reading LISP for a second. Actually what you have here is
  50. precisely equivalent to:
  51.  
  52. return (fopen (filename, mode));
  53.  
  54. Yes, you do check against NULL but handle it by just returning NULL.
  55.  
  56. |> }
  57. |> /* so how do I add the functionality descrbed above? */
  58. |> /* I believe tmpnam() if a non-ansi function ... is it? */
  59.  
  60. You're on safe ground...Both tmpfile() and tmpnam() are ANSI C so long
  61. as you #include <stdio.h>.
  62.  
  63. Given this you may want to use tmpfile() which returns a FILE * with wb+
  64. mode that is guaranteed to be removed upon being either closed or upon
  65. program exit.
  66.  
  67. Maybe I didn't understand your question...Please elaborate if I'm off base.
  68. -- 
  69. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  70.  
  71. Jonas J. Schlein  (schlein@gl.umbc.edu)
  72.